package com.timoon.manager.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.timoon.app.utils.GlobalRandom;
import com.timoon.manager.panel.passage.PassageManagerService;
import com.timoon.sample.dao.InfoSortDAO;
import com.timoon.sample.dao.PassageDAO;
import com.timoon.sample.dao.PassagePicDAO;
import com.timoon.sample.dao.model.InfoSort;
import com.timoon.sample.dao.model.InfoSortExample;
import com.timoon.sample.dao.model.Passage;
import com.timoon.sample.dao.model.PassageExample;
import com.timoon.sample.dao.model.PassagePic;
import com.timoon.sample.dao.model.PassagePicExample;

/**
 *<p>功能:</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

@Service(value="manager.passage")
public class PassageServiceImpl implements PassageManagerService {

	@Resource
	private InfoSortDAO infoSortDAOImpl;
	
	@Resource
	private PassageDAO passageDAOImpl;
	
	@Resource
	private PassagePicDAO passagePicDAOImpl;
	
	@Override
	public List<InfoSort> getInfoSortsByType(final Integer type) {
		// TODO Auto-generated method stub
		return this.infoSortDAOImpl.selectByExample(new InfoSortExample(){{
			this.createCriteria().andTypeEqualTo(type);
		}});
	}

	@Override
	public List<Passage> getPassagesByType(final Integer type) {
		// TODO Auto-generated method stub
		return this.passageDAOImpl.selectByExampleWithoutBLOBs(new PassageExample(){{
			this.createCriteria().andStatusNotEqualTo(0).andTypeEqualTo(type);
		}});
	}

	@Override
	public void addOrUpadtePassage(Passage passage, List<PassagePic> passagePics) {
		// TODO Auto-generated method stub
		
		final Integer tempStatus=GlobalRandom.getRandom();
		
		passage.setStatus(tempStatus);
		if(passage.getId()==null){
			if(passage.getUpdateDate()==null){
				passage.setUpdateDate(new Date());
			}
			if(passage.getReadCount()==null){
				passage.setReadCount(0);
			}
			this.passageDAOImpl.insert(passage);
		}else{
			this.passageDAOImpl.updateByPrimaryKeySelective(passage);
		}
		
		if(passagePics!=null&&passagePics.size()>0){
			List<Passage> p=this.passageDAOImpl.selectByExampleWithoutBLOBs(new PassageExample(){{
				this.createCriteria().andStatusEqualTo(tempStatus);
			}});
			
			Integer passageId=p.get(0).getId();
			for(PassagePic pic:passagePics){
				pic.setPassageId(passageId);
				this.passagePicDAOImpl.insert(pic);
			}
		}
	}

	@Override
	public List<PassagePic> getPassagePicsByPassageId(Integer passageId) {
		// TODO Auto-generated method stub
		if(passageId==null){
			passageId=-1;
		}
		
		final Integer type=passageId;
		
		return this.passagePicDAOImpl.selectByExample(new PassagePicExample(){{
			this.createCriteria().andPassageIdEqualTo(type);
		}});
	}

	@Override
	public void delPassageByIds(List<String> ids) {
		// TODO Auto-generated method stub
		if(ids!=null){
			for(String id:ids){
				Passage p=this.passageDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
				p.setStatus(0);
				this.passageDAOImpl.updateByPrimaryKeySelective(p);
				
				final Integer passageId=p.getId();
				this.passagePicDAOImpl.deleteByExample(new PassagePicExample(){{
					this.createCriteria().andPassageIdEqualTo(passageId);
				}});
			}
		}
	}

	@Override
	public void delPassagePicById(Integer id) {
		// TODO Auto-generated method stub
		this.passagePicDAOImpl.deleteByPrimaryKey(id);
	}

}
